// By Robalo / TPWCAS

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "aisuppress\fn_moveToCover.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_unit","_cover","_coverArray","_coverPosition","_coverDist","_coverTarget","_cPos","_vPos","_debug_flag","_dist","_shooter","_continue","_logOnce","_startTime","_checkTime","_stopped","_tooFar","_tooLong","_elapsedTime"
		,"_bullet_cnt","_start_cnt"];

_unit 			=	_this select 0;
_coverArray 	=	_this select 1;
_shooter 		=	_this select 2;

_cover 			=	_coverArray select 0;
_coverPosition 	= 	_coverArray select 1;

doStop _unit;
_unit forceSpeed -1;
_unit doMove _coverPosition;
_unit setUnitPos "AUTO"; 
_unit setUnitPosWeak "middle";
(group _unit) setSpeedMode "FULL";

_coverDist = round ( _unit distance _coverPosition );

_stopped = false;
_continue = true;
_logOnce = true;

_startTime = time;
_checkTime =  (_startTime + (1.7 * _coverDist) + 9);

_debug_flag = objNull;
if (GVAR(DEBUG_MOVE)) then {
	_debug_flag = "Flag_FD_Red_F" createVehicle _coverPosition;
	_debug_flag setPosATL _coverPosition;
};
_start_cnt = _unit getVariable [QGVAR(BULLET_COUNT),0];
while { _continue } do  {
	_dist = round ( _unit distance _coverPosition );
	if ( !( unitReady _unit ) && ( alive _unit ) && ( _dist > 1.25 ) ) then {
		//if unit takes too long to reach cover or moves too far out stop at current location
		_tooFar = ( _dist > ( _coverDist + 10 ));
		_tooLong = ( time >  _checkTime );
		_elapsedTime = time - _checkTime;
		
		_bullet_cnt = _unit getVariable [QGVAR(BULLET_COUNT),0];
		if ((_dist > (_coverDist / 3)) && {_bullet_cnt >= GVAR(MIN_BULLET_FOR_PRONE)}) then {
			_unit setUnitPos "DOWN";
		};
		
		if (_bullet_cnt > (_start_cnt * 1.5)) then {
			_start_cnt = _start_cnt * 1.5;
			_unit forceSpeed -1;
			(group _unit) setSpeedMode "FULL";
			_unit doMove _coverPosition;
		};
		
		if (_dist < (_coverDist / 3)) then {
			_unit setUnitPos "AUTO";
			(group _unit) setSpeedMode "FULL";
		};
		
		if ( _tooFar || _tooLong ) then {
			TRACE_1("Unit [%1] moving wrong way to cover [%2]: [%3] m - drop here - tooFar: [%4] - tooLong: [%5] - ([%6] seconds)",ARR_6(_unit, _cover, _dist, _tooFar, _tooLong, _elapsedTime));
			_unit setUnitPos "AUTO";
			_coverPosition = getPosATL _unit;
			_unit forceSpeed -1;
			_unit doMove _coverPosition;
			_unit setVariable ["UNIT_STATE",0,true];
			_stopped = true;
			_continue = false;
		};
		sleep 0.5;
	} else {	
		_continue = false;
	};
}; 

if ( !( _stopped) ) then {
	doStop _unit;
	_unit setUnitPos "AUTO"; 
	_unit setUnitPosWeak "middle";
	sleep (10 + random 15);
		
	//doMove:
	//Order the given unit(s) to move to the given position (without radio messages). 
	//After reaching his destination, the unit will immediately return to formation (if in a group); or order his group to form around his new position (if a group leader). 
	_coverPosition = getPosATL _unit;
	_unit forceSpeed -1;
	_unit doMove _coverPosition;
} else {
	TRACE("Did not reach Cover");
};

if (!isNull _debug_flag) then {
	deleteVehicle _debug_flag;
};

TRACE("End");
TMFPROFILERSTOP;
